<template>
  <div v-loading="loading" class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('新签合同') }}</span>
    </div>
    <el-form v-if="queryPramsHidden" ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('项目名称')" prop="dealName">
        <el-input
          v-model="queryParams.dealName"
          :placeholder="$t('项目名称')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('申请时间')">
        <DatePicker
          v-model="newDateRange"
          type="daterange"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t("搜索") }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("重置") }}</el-button>
        <el-button
          v-hasPermi="['deal:contract:export']"
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="exportHandle"
        >{{ $t("导出") }}</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="dataList" border>
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('项目名称')" prop="dealName" align="center" show-overflow-tooltip>
        <template slot-scope="scope">
          <router-link
            target="_blank"
            :to="{path:'/deal/home/dealBaseHome',query:{id:scope.row.dealId, companyId: scope.row.companyId}}"
            class="link_a"
          >{{ scope.row.dealName }}</router-link>
        </template>
      </el-table-column>
      <el-table-column :label="$t('合同名称')" prop="contractName" align="center" show-overflow-tooltip />
      <el-table-column :label="$t('申请人')" prop="applyUserName" align="center" show-overflow-tooltip width="110" />
      <el-table-column :label="$t('申请时间')" prop="applyTime" align="center" show-overflow-tooltip width="110">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.applyTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('收付款类型')" prop="paymentType" align="center" :formatter="paymentTypeFormat" show-overflow-tooltip width="110" />
      <el-table-column :label="$t('是否用印')" prop="seal" align="center" :formatter="sealFormat" show-overflow-tooltip width="110" />
      <el-table-column :label="$t('合同金额') + $t('金额单位')" prop="contractAmount" align="center" show-overflow-tooltip width="110">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.contractAmount, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('操作')" align="center" width="80">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['payment:report:view']"
            size="small"
            type="text"
            icon="el-icon-view"
            @click="viewHandle(scope.row)"
          >{{ $t('查看') }}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改合同信息对话框 -->
    <el-dialog v-if="showContract" :title="title" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" append-to-body custom-class="dialogForm">
      <dealContractForm ref="dealContractForm" />
    </el-dialog>

    <!-- 添加或修改合同信息对话框 -->
    <el-dialog v-if="!showContract" :title="title" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" append-to-body custom-class="dialogForm">
      <dealContractDebtForm ref="dealContractDebtForm" />
    </el-dialog>
  </div>
</template>

<script>
import { findNewContract } from '@/api/deal/contract/dealContract'
import dealContractForm from '@/views/deal/contract/dealContractForm'
import dealContractDebtForm from '@/views/deal/contract/debt/dealContractDebtForm'
import { downLoadExcel } from '@/utils/excelDownload'
export default {
  components: { dealContractDebtForm, dealContractForm },
  props: {
    dateRange: {
      type: Array,
      default: undefined,
      desc: '日期范围'
    },
    queryPramsHidden: {
      type: Boolean,
      default: true,
      desc: '是否隐藏查询条件'
    }
  },
  data() {
    return {
      dataList: [],
      // 遮罩层
      loading: false,
      // 合伙企业查询参数
      queryParams: {
        dealName: undefined,
        pageNum: 1,
        pageSize: 10
      },
      total: 0,
      // 是否用印字典
      sealOptions: [],
      formOpen: false,
      title: this.$t('项目合同'),
      // 收付款类型
      paymentTypeOptions: [],
      showContract: false,
      newDateRange: []
    }
  },
  watch: {
    dateRange: {
      handler(val) {
        this.newDateRange = val
      },
      deep: true
    }
  },
  created() {
    this.getList()
    this.getDicts('sys_yes_no').then(response => {
      this.sealOptions = response.data
    })
    this.getDicts('collection_payment_no').then(response => {
      this.paymentTypeOptions = response.data
    })
  },
  methods: {
    getList() {
      this.loading = true
      findNewContract(this.addDateRange(this.queryParams, this.newDateRange)).then(response => {
        this.dataList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    queryHandle() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.newDateRange = undefined
      this.queryHandle()
    },
    // 收付款类型 字典翻译
    paymentTypeFormat(row) {
      return this.selectDictLabel(this.paymentTypeOptions, row.paymentType)
    },
    // 是否用印 字典翻译
    sealFormat(row) {
      return this.selectDictLabel(this.sealOptions, row.seal)
    },
    viewHandle(row) {
      this.showContract = row.contractType === 'equity'
      this.formOpen = true
      this.title = this.$t('项目合同')
      this.$nextTick(() => {
        const data = {
          id: row.contractId,
          dealId: row.dealId
        }
        // 调用form页面的 init 方法初始化数据
        if (row.contractType === 'equity') {
          this.$refs.dealContractForm.init(data)
        } else {
          this.$refs.dealContractDebtForm.init(data)
        }
      })
    },
    exportHandle() {
      downLoadExcel('/contract/dealContract/findNewContract/exportReport', this.queryParams)
    }
  }
}
</script>

<style scoped>

</style>
